//
//  RegisterVC.swift
//  ShareUmbrella
//
//  Created by keng fu on 2017/8/9.
//  Copyright © 2017年 尧尚信息科技. All rights reserved.
//
//  Roger:18684909663

import Foundation
import UIKit
import YSKit
import TSMessages

class RegisterVC: YSBaseViewController {
    var time : Int = 0
    var timer : Timer!
    
    @IBOutlet weak var tf_name: UITextField!
    
    @IBOutlet weak var tf_code: UITextField!

    @IBOutlet weak var tf_pwd: UITextField!
    
    @IBOutlet weak var tf_pwdAg: UITextField!
    
    @IBOutlet weak var btn_verify: UIButton!
    
    @IBOutlet weak var btn_read: UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        self.btn_read.ys.setImageAndTitle(imageName: "icon_read", title: "我已经阅读并同意", type: .left, space: 5.0)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.isNavigationBarHidden = true;
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.isNavigationBarHidden = false;
    }

    @IBAction func backAction(_ sender: Any) {
        self.navigationController?.popViewController(animated: true)
    }
    
    
    @IBAction func sendVerifyCode(_ sender: UIButton) {
        if !(tf_name.text?.isPhone("CN"))! {
            TSMessage.showNotification(withTitle: I18n("VC.login.notphone"), type: .warning)
            return
        }
        
        sender.isEnabled = false
        self.time = 60
        self.timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true, block: { (Timer) in
            self.btn_verify.setTitle(String.init(format: "%lds", self.time), for: UIControlState.normal)
            if self.time == 0 {
                self.time = 60
                self.btn_verify .setTitle(I18n("VC.login.verifybtn.title"), for: UIControlState.normal)
                self.btn_verify.isEnabled = true
                self.timer.invalidate()
            }else{
                self.time -= 1
            }
        })
    }
    
    @IBAction func registerAction(_ sender: UIButton) {
        if !self.verifyInput() {
            return
        }
        
    }
    
    func verifyInput() -> Bool {
        if tf_name.text?.characters.count == 0 {
            TSMessage.showNotification(withTitle: I18n("VC.login.noname"), type: .warning)
            return false;
        }
        
        if tf_pwd.text?.characters.count == 0, tf_pwdAg.text?.characters.count == 0{
            TSMessage.showNotification(withTitle: I18n("VC.login.nopwd"), type: .warning)
            return false;
        }
        
        if (tf_pwd.text?.characters.count)! < 6, (tf_pwdAg.text?.characters.count)! < 6 {
            TSMessage.showNotification(withTitle: I18n("VC.login.pwdlength"), type: .warning)
            return false;
        }
        
        if (tf_pwd.text?.characters.count)! > 20, (tf_pwdAg.text?.characters.count)! > 20 {
            TSMessage.showNotification(withTitle: I18n("VC.login.pwdlengthtoolong"), type: .warning)
            return false;
        }
        
        if !(tf_name.text?.isPhone("CN"))! {
            TSMessage.showNotification(withTitle: I18n("VC.login.notphone"), type: .warning)
            return false;
        }
        
        if !(tf_code.text?.characters.count == 0) {
            TSMessage.showNotification(withTitle: I18n("VC.login.nocode"), type: .warning)
            return false;
        }
        
        if (tf_pwdAg.text != tf_pwd.text) {
            TSMessage.showNotification(withTitle: I18n("VC.login.pwdnotsame"), type: .warning)
            return false;
        }
        
        return true;
    }
    
    
    @IBAction func readAction(_ sender: UIButton) {
    }
    
    @IBAction func agreeAction(_ sender: UIButton) {
    }
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}



